﻿using System;
using Microsoft.SharePoint.WebPartPages;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace VPLink.WebParts
{
    public class BehaviorToolPart : ToolPart
    {
        private CheckBox cbBehavior;

        /// <summary>
        /// Constructor for the class.
        /// </summary>        
        public BehaviorToolPart()
        {
            this.Title = "Mode de fonctionnement :";
            this.PreRender += new EventHandler(BehaviorToolPart_PreRender);
        }

        /// <summary>
        /// Create dropdownlist to display Announcement lists
        /// </summary>
        override protected void CreateChildControls()
        {
            try
            {
                Label lblTitre = new Label();
                lblTitre.Text = "Si vous cochez cette case, les actualités affichées seront directement publiées depuis la page d'accueil du portail, sinon ce sont les actualités issues de la liste ci-dessous qui seront affichées.";
                Controls.Add(lblTitre);

                LiteralControl lit2 = new LiteralControl("<BR>&nbsp;&nbsp;&nbsp;");
                Controls.Add(lit2);
                cbBehavior = new CheckBox();
                Controls.Add(cbBehavior);
            }
            catch (Exception e)
            {
                Log.Write(e.Message + "\r\n" + e.StackTrace, System.Diagnostics.EventLogEntryType.Error);
            }
        }

        ///	<summary>
        ///	This method is called by the ToolPane object to apply property changes to the selected Web Part. 
        ///	</summary>
        override public void ApplyChanges()
        {
            // Apply property values here.
            AnnounceListViewWebPart currentWebPart = (AnnounceListViewWebPart)ParentToolPane.SelectedWebPart;
            if (currentWebPart != null)
            {
                currentWebPart.Behavior = this.cbBehavior.Checked;
            }
        }

        ///	<summary>
        ///	Initializes ToolPart values. 
        ///	</summary>
        private void BehaviorToolPart_PreRender(object sender, EventArgs ea)
        {
            try
            {
                AnnounceListViewWebPart currentWebPart = (AnnounceListViewWebPart)ParentToolPane.SelectedWebPart;
                if (currentWebPart != null)
                {
                    bool behavior = currentWebPart.Behavior;
                    this.cbBehavior.Checked = behavior;
                }
            }
            catch (Exception e)
            {
                Log.Write(e.Message + "\r\n" + e.StackTrace, System.Diagnostics.EventLogEntryType.Error);
            }
        }
    }
}